/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.view;

import entity.Contact;
import entity.ContactCompany;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import session.ContactCompanyFacade;

/**
 *
 * @author owtee
 */
public class ApplicationReqContact extends HttpServlet {

    @EJB
    private ContactCompanyFacade contactCompanyFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            try {
                String comId = request.getParameter("comId");

                ContactCompany conCom = contactCompanyFacade.find(Integer.parseInt(comId));

                List<Contact> conLst = (List<Contact>) conCom.getContactCollection();

                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
                //creating a new instance of a DOM to build a DOM tree.
                Document doc = docBuilder.newDocument();
                //new XmlServlet().createXmlTree(doc);

                Element msg = doc.createElement("Messenger");
                doc.appendChild(msg);
                // Create element company
                Element tagCompany = doc.createElement("Company");
                msg.appendChild(tagCompany);
                Element tagComId = doc.createElement("Id");
                tagCompany.appendChild(tagComId);
                Text txtComId = doc.createTextNode(Integer.toString(conCom.getConComId()));
                tagComId.appendChild(txtComId);
                Element tagComName = doc.createElement("Name");
                tagCompany.appendChild(tagComName);
                Text txtComName = doc.createTextNode(conCom.getConComName());
                tagComName.appendChild(txtComName);
                Element tagComAddr = doc.createElement("Address");
                tagCompany.appendChild(tagComAddr);
                Text txtComAddr = doc.createTextNode(conCom.getConComAddress());
                tagComAddr.appendChild(txtComAddr);

                // Create element summary contract data            
                Element tagConSize = doc.createElement("ContactSize");
                tagConSize.setAttribute("NumRecData", Integer.toString(conLst.size()));
                msg.appendChild(tagConSize);
                // Create element contract
                for (Contact con : conLst) {
                    Element tagContact = doc.createElement("Contact");
                    msg.appendChild(tagContact);
                    Element tagConId = doc.createElement("Id");
                    tagContact.appendChild(tagConId);
                    Text txtConId = doc.createTextNode(con.getConId().toString());
                    tagConId.appendChild(txtConId);
                    Element tagConName = doc.createElement("Name");
                    tagContact.appendChild(tagConName);
                    Text txtConName = doc.createTextNode(con.getConName());
                    tagConName.appendChild(txtConName);
                    Element tagConTel = doc.createElement("Telephone");
                    tagContact.appendChild(tagConTel);
                    Text txtConTel = doc.createTextNode(con.getConTel());
                    tagConTel.appendChild(txtConTel);
                }

                //TransformerFactory instance is used to create Transformer objects.
                TransformerFactory factory = TransformerFactory.newInstance();
                Transformer transformer = factory.newTransformer();

                transformer.setOutputProperty(OutputKeys.INDENT, "yes");

                // create string from xml tree
                StringWriter sw = new StringWriter();
                StreamResult result = new StreamResult(sw);
                DOMSource source = new DOMSource(doc);
                transformer.transform(source, result);
                String xmlString = sw.toString().replace("&#13;", "");

                out.println(xmlString);

            } catch (Exception ex) {
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
